@model IEnumerable<UpdaterManagement.Models.VersionRelease>

<nav aria-label="breadcrumb" class="breadcrumb-ace">
    <ol class="breadcrumb">
        <li class="breadcrumb-item"><a href="/">首页</a></li>
        <li class="breadcrumb-item active">版本管理</li>
    </ol>
</nav>

<div class="page-header">
    <div class="d-flex justify-content-between align-items-center">
        <div>
            <h1 class="page-title">
                <i class="bi bi-tags text-primary me-2"></i>版本管理
            </h1>
            <p class="page-subtitle">管理产品的所有版本发布，支持文件上传和版本控制</p>
        </div>
        <a href="/Version/Create" class="btn btn-ace-primary">
            <i class="bi bi-plus-circle me-2"></i>新增版本
        </a>
    </div>
</div>

<div class="card-ace mb-3">
    <div class="card-body">
        <form method="get" class="row g-3" id="searchForm">
            <div class="col-md-3">
                <label class="form-label-ace">产品筛选</label>
                <select class="form-select form-control-ace" name="productId">
                    <option value="">全部产品</option>
                    @foreach (var product in ViewBag.Products as List<UpdaterManagement.Models.Product>)
                    {
                        <option value="@product.Id" selected="@(product.Id == ViewBag.ProductId)">@product.Name</option>
                    }
                </select>
            </div>
            <div class="col-md-3">
                <label class="form-label-ace">企业筛选</label>
                <select class="form-select form-control-ace" name="enterpriseId">
                    <option value="">全部企业</option>
                    @foreach (var enterprise in ViewBag.Enterprises as List<UpdaterManagement.Models.Enterprise>)
                    {
                        <option value="@enterprise.Id" selected="@(enterprise.Id == ViewBag.EnterpriseId)">@enterprise.Name</option>
                    }
                </select>
            </div>
            <div class="col-md-2">
                <label class="form-label-ace">&nbsp;</label>
                <div>
                    <button type="submit" class="btn btn-ace-primary me-2">
                        <i class="bi bi-search me-1"></i>查询
                    </button>
                    <a href="/Version" class="btn btn-outline-secondary">
                        <i class="bi bi-arrow-clockwise me-1"></i>重置
                    </a>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="row mb-4">
    <div class="col-xl-3 col-md-6">
        <div class="stat-card">
            <div class="stat-icon text-primary">
                <i class="bi bi-collection"></i>
            </div>
            <div class="stat-number text-primary">@ViewBag.TotalCount</div>
            <div class="stat-label">总版本数</div>
        </div>
    </div>
    <div class="col-xl-3 col-md-6">
        <div class="stat-card">
            <div class="stat-icon" style="color: var(--ace-success);">
                <i class="bi bi-check-circle"></i>
            </div>
            <div class="stat-number" style="color: var(--ace-success);">@Model.Count(v => v.IsActive)</div>
            <div class="stat-label">活跃版本</div>
        </div>
    </div>
    <div class="col-xl-3 col-md-6">
        <div class="stat-card">
            <div class="stat-icon" style="color: var(--ace-info);">
                <i class="bi bi-boxes"></i>
            </div>
            <div class="stat-number" style="color: var(--ace-info);">@Model.GroupBy(v => v.ProductId).Count()</div>
            <div class="stat-label">涉及产品</div>
        </div>
    </div>
    <div class="col-xl-3 col-md-6">
        <div class="stat-card">
            <div class="stat-icon" style="color: var(--ace-warning);">
                <i class="bi bi-file-earmark-zip"></i>
            </div>
            <div class="stat-number" style="color: var(--ace-warning);">@Model.Where(v => !string.IsNullOrEmpty(v.FilePath)).Count()</div>
            <div class="stat-label">有文件版本</div>
        </div>
    </div>
</div>

@if (Model.Any())
{
    <div class="card-ace">
        <div class="card-header">
            <i class="bi bi-table me-2"></i>版本列表
            <span class="badge badge-ace-secondary ms-2">@ViewBag.TotalCount 个版本</span>
            @if (ViewBag.ProductId != null)
            {
                var selectedProduct = (ViewBag.Products as List<UpdaterManagement.Models.Product>)?.FirstOrDefault(p => p.Id == ViewBag.ProductId);
                if (selectedProduct != null)
                {
                    <span class="badge badge-ace-info ms-2">产品: @selectedProduct.Name</span>
                }
            }
            @if (ViewBag.EnterpriseId != null)
            {
                var selectedEnterprise = (ViewBag.Enterprises as List<UpdaterManagement.Models.Enterprise>)?.FirstOrDefault(e => e.Id == ViewBag.EnterpriseId);
                if (selectedEnterprise != null)
                {
                    <span class="badge badge-ace-warning ms-2">企业: @selectedEnterprise.Name</span>
                }
            }
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-ace mb-0">
                    <thead>
                        <tr>
                            <th>产品信息</th>
                            <th>企业版本</th>
                            <th>版本号</th>
                            <th>描述</th>
                            <th>创建时间</th>
                            <th>文件</th>
                            <th>状态</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var item in Model)
                        {
                            <tr>
                                <td>
                                    <div>
                                        <strong class="text-dark">@item.ProductName</strong>
                                        <br><small class="text-muted">@item.ProductId</small>
                                    </div>
                                </td>
                                <td>
                                    <span class="badge badge-ace-secondary">@(item.Enterprise?.Name ?? "未知")</span>
                                </td>
                                <td>
                                    <span class="badge badge-ace-primary">@item.Version</span>
                                </td>
                                <td>
                                    @if (!string.IsNullOrEmpty(item.Description))
                                    {
                                        <span title="@item.Description">
                                            @(item.Description.Length > 50 ? item.Description.Substring(0, 50) + "..." : item.Description)
                                        </span>
                                    }
                                    else
                                    {
                                        <span class="text-muted">无描述</span>
                                    }
                                </td>
                                <td>
                                    <small class="text-muted">@item.CreatedTime.ToString("MM-dd HH:mm")</small>
                                </td>
                                <td>
                                    <div class="d-flex gap-1">
                                        @if (!string.IsNullOrEmpty(item.FilePath))
                                        {
                                            <i class="bi bi-file-zip text-success" title="有版本文件"></i>
                                        }
                                        @if (!string.IsNullOrEmpty(item.ManualPath))
                                        {
                                            <i class="bi bi-file-text text-info" title="有操作手册"></i>
                                        }
                                        @if (string.IsNullOrEmpty(item.FilePath) && string.IsNullOrEmpty(item.ManualPath))
                                        {
                                            <span class="text-muted">-</span>
                                        }
                                    </div>
                                </td>
                                <td>
                                    @if (item.IsActive)
                                    {
                                        <span class="badge badge-ace-success">
                                            <i class="bi bi-check-circle me-1"></i>活跃
                                        </span>
                                    }
                                    else
                                    {
                                        <span class="badge badge-ace-secondary">
                                            <i class="bi bi-pause-circle me-1"></i>停用
                                        </span>
                                    }
                                </td>
                                <td>
                                    <div class="btn-group btn-group-sm">
                                        <a href="/Version/Details/@item.Id" class="btn btn-outline-info btn-sm" title="查看详情">
                                            <i class="bi bi-eye"></i>
                                        </a>
                                        <a href="/Version/Edit/@item.Id" class="btn btn-outline-primary btn-sm" title="编辑版本">
                                            <i class="bi bi-pencil"></i>
                                        </a>
                                        <form method="post" action="/Version/Delete/@item.Id" class="d-inline">
                                            <button type="button" class="btn btn-outline-danger btn-sm" title="删除版本" 
                                                    onclick="showDeleteConfirm('@item.Version', this.form)">
                                                <i class="bi bi-trash"></i>
                                            </button>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
        </div>
        
        <partial name="_Pagination" />
    </div>
}
else
{
    <div class="text-center py-5">
        <div class="mb-3">
            <i class="bi bi-inbox" style="font-size: 48px; color: #ccc;"></i>
        </div>
        <h5 class="text-muted">暂无版本数据</h5>
        <p class="text-muted">请点击上方按钮添加第一个版本</p>
        <a href="/Version/Create" class="btn btn-ace-primary">
            <i class="bi bi-plus-circle me-2"></i>立即添加
        </a>
    </div>
}

<script>
function showDeleteConfirm(version, form) {
    if (confirm(`确定要删除版本 "${version}" 吗？此操作不可恢复！`)) {
        form.submit();
    }
}
</script>